<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<meta name="decorator" content="sysDefault"
	content="text/html; charset=utf-8" />
<script type="text/javascript">
	$(document)
			.ready(
					function() {
						$("#key").focus();
						$("#dataForm").validate(
								{
									submitHandler : function(form) {
										Oosp_Loading('正在提交，请稍等...');
										form.submit();
									},
									errorContainer : "#messageBox",
									errorPlacement : function(error, element) {
										$("#messageBox").text("输入有误，请先更正。");
										if (element.is(":checkbox")
												|| element.is(":radio")
												|| element.parent().is(
														".input-append")) {
											error.appendTo(element.parent()
													.parent());
										} else {
											error.insertAfter(element);
										}
									}
								});

						//找到当前最大行号
						max_tr = $("#hidden_tr").closest("table").find("tr").length - 2;
					});

	function add_hidden() {
		var tr = $("#hidden_tr").clone();
		var table = $("#hidden_tr").closest("table");
		//替换所有的hidden，方便spring的form绑定
		var inputs = tr.find("input");
		max_tr++;
		inputs.each(function() {
			this.id = "codeDList" + max_tr + this.id;
			this.name = "codeDList[" + max_tr + "]" + this.name;
		});
		tr.show();
		table.append(tr);
	}
	function delete_tr(t) {
		var tr = $(t).closest("tr");
		var id = tr.find("input :first").val();
		if (id != 0) {
			var val = $("#deleteList").val();
			if (val == "") {
				$("#deleteList").val(id);
			} else {
				$("#deleteList").val(val + "," + id);
			}
		}
		tr.remove();

	}
</script>
</head>
<body>
	<div class="container-fluid">
		<div class="row">
			<div class="col-xs-12">
				<div class="panel panel-default">

					<div class="panel-heading">
						<h3 class="panel-title">
							编辑数据 <a class="btn btn-default btn-sm pull-right" role="button"
								href="javascript:void(0)" onclick="history.go(-1)">返回</a>
						</h3>
					</div>
					<form:form id="dataForm" modelAttribute="code"
						action="${contextPath}/pospay/code/save" method="post"
						class="form-horizontal" role="form">
						<div class="panel-body">

							<form:hidden path="id" />
							<tags:message content="${message}" />
							<div class="form-group">
								<label for="typeNo" class="col-sm-2 control-label">代码类型编号:</label>
								<div class="col-sm-2">
									<form:input id="typeNo" path="typeNo" maxlength="20"
										class="form-control" />
								</div>
							</div>
							<div class="form-group">
								<label for="typeName" class="col-sm-2 control-label">代码类型名称:</label>
								<div class="col-sm-2">
									<form:input id="typeName" path="typeName" maxlength="20"
										class="form-control" />
								</div>
							</div>
							<div class="form-group">
								<label for="createBy" class="col-sm-2 control-label">创建人:</label>
								<div class="col-sm-2">
									<form:input id="createBy" path="createBy" maxlength="20"
										class="form-control" />
								</div>
							</div>


						</div>

						<!-- 新增明细 -->
						<div class="panel panel-default">
							<div class="panel-heading">
								<h3 class="panel-title">
									编辑明细 <input onclick="add_hidden();" type="button"
										class="btn btn-success btn-sm pull-right" value="新增明细"></input>
								</h3>
							</div>
							<table class="table table-striped table-bordered table-hover">

								<thead>
									<tr>
										<th>代码编号</th>
										<th>代码描述</th>
										<th>序号</th>
										<th>创建人</th>

										<th>操作</th>

									</tr>
								</thead>
								<tbody>
									<c:forEach items="${code.codeDList}" varStatus="var">
										<tr>
											<form:hidden path="codeDList[${var.index}].id" maxlength="20"
												class="form-control required" />
											<form:hidden path="codeDList[${var.index}].codeId"
												maxlength="20" class="form-control required" />
											<td><form:input path="codeDList[${var.index}].codeNo"
													maxlength="20" class="form-control required" /></td>
											<td><form:input path="codeDList[${var.index}].codeName"
													maxlength="20" class="form-control required" /></td>
											<td><form:input path="codeDList[${var.index}].xh"
													maxlength="20" class="form-control number" /></td>
											<td><form:input path="codeDList[${var.index}].createBy"
													maxlength="20" class="form-control" /></td>

											<td><input type="button" onclick="delete_tr(this)"
												value="删除" class="btn btn-default btn-sm"></input></td>
										</tr>
									</c:forEach>
									<!-- 隐藏行，用于新增明细start -->
									<tr style="display: none" id="hidden_tr">
										<input type="hidden" maxlength="20" value="0"
											class="form-control required" name=".id" id=".id">
										<input type="hidden" maxlength="20" value="${code.id }"
											class="form-control required" name=".codeId" id=".codeId">
										<td><input type="text" maxlength="20" value=""
											class="form-control required" name=".codeNo" id=".codeNo"></td>
										<td><input type="text" maxlength="20" value=""
											class="form-control required" name=".codeName" id=".codeName"></td>
										<td><input type="text" maxlength="20" value=""
											class="form-control" name=".xh" id=".xh"></td>
										<td><input type="text" maxlength="20" value=""
											class="form-control" name=".createBy" id=".createBy"></td>
										<td><input type="button" onclick="delete_tr(this)"
											value="删除" class="btn btn-default btn-sm"></td>
									</tr>
									<!-- 隐藏行，用于新增明细end -->
									<input type="hidden" name="deleteList" id="deleteList">
								</tbody>
							</table>
						</div>
						<!-- 新增明細end -->


						<div class="form-group">
							<div class="col-sm-offset-1 col-sm-11">
								<input class="btn btn-primary" type="submit" value="保 存" /> <input
									class="btn btn-default" type="button" value="返 回"
									onclick="history.go(-1)" />
							</div>
						</div>
					</form:form>

				</div>


			</div>
		</div>
	</div>
</body>
</html>